package com.imelite.qyh.action;

import com.imelite.qyh.base.result.ResultBean;
import com.imelite.qyh.base.result.ResultBeanFactory;
import com.imelite.qyh.model.EliteCRMView_OrgUser;
import com.imelite.qyh.model.OrgDep;
import com.imelite.qyh.model.OrgEmployee;
import com.imelite.qyh.service.EliteCRMView_OrgUserService;
import com.imelite.qyh.service.OrgDepService;
import com.imelite.qyh.util.BaseResponseUtil;
import com.imelite.qyh.util.BaseStringUtil;
import com.imelite.qyh.util.Encrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class UserAction {
    @Autowired
    private EliteCRMView_OrgUserService eliteCRMView_orgUserService;
    @Autowired
    private OrgDepService orgDepService;
    @RequestMapping("/com/imelite/qyh/UserAction/setUserWxInfo")
    public String setUserWxInfo(HttpServletRequest request, HttpServletResponse response,String WxUserId,String url,String params,String AgentId){
        System.out.println("跳转注册获取WxUserId="+WxUserId);
        String loadUrl=url.replace("http://localhost:8080", "http://qyh.imeliteonline.com")+"?"+params;
        System.out.println("注册后跳转地址="+loadUrl);
        String reqUrl="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxaad2914913724470&redirect_uri="+loadUrl+"&response_type=code&scope=SCOPE&agentid="+AgentId+"&state=STATE#wechat_redirect";
        request.setAttribute("url",reqUrl);
        request.setAttribute("WxUserId",WxUserId);
        return "/binding";
    }


    @RequestMapping("/com/imelite/qyh/UserAction/updateUser")
    public void updateUser(HttpServletRequest request, HttpServletResponse response,String username,String pwd,String wxId){
        ResultBean resultBean=null;
        if(BaseStringUtil.isBlank(username)||BaseStringUtil.isBlank(pwd)){
            resultBean=new ResultBean("登陆账户或密码不能为空！");
        }else{
            EliteCRMView_OrgUser eliteCRMView_orgUser=new EliteCRMView_OrgUser();
            Encrypt encrypt=new Encrypt();
            eliteCRMView_orgUser.setSlogin(username);
            eliteCRMView_orgUser.setSpassword(encrypt.enCode(pwd));

            List<EliteCRMView_OrgUser> eliteCRMView_orgUserList=eliteCRMView_orgUserService.selectByForm(eliteCRMView_orgUser);
            if(eliteCRMView_orgUserList.isEmpty() || eliteCRMView_orgUserList.size()==0){
                resultBean=new ResultBean("登陆账户或密码错误！");
            }else{
                OrgEmployee orgEmployee=new OrgEmployee();
                orgEmployee.setId(eliteCRMView_orgUserList.get(0).getId());
                orgEmployee.setWxcode(wxId);
                resultBean=eliteCRMView_orgUserService.update(orgEmployee);
            }
        }
        BaseResponseUtil.responseAjaxJson(response, resultBean);
    }

    @RequestMapping("/com/imelite/qyh/UserAction/getUserByLogin")
    public void getUserByLogin(HttpServletRequest request, HttpServletResponse response,String username,String pwd){
        ResultBean resultBean=null;
        if(BaseStringUtil.isBlank(username)||BaseStringUtil.isBlank(pwd)){
            resultBean=new ResultBean("登陆账户或密码不能为空！");
        }else{
            EliteCRMView_OrgUser eliteCRMView_orgUser=new EliteCRMView_OrgUser();
            Encrypt encrypt=new Encrypt();
            eliteCRMView_orgUser.setSlogin(username);
            eliteCRMView_orgUser.setSpassword(encrypt.enCode(pwd));

            List<EliteCRMView_OrgUser> eliteCRMView_orgUserList=eliteCRMView_orgUserService.selectByForm(eliteCRMView_orgUser);
            if(eliteCRMView_orgUserList.isEmpty() || eliteCRMView_orgUserList.size()==0){
                resultBean=new ResultBean("登陆账户或密码错误！");
            }else{
                String depId=eliteCRMView_orgUserList.get(0).getIddep();
                OrgDep orgDep = orgDepService.selectByPrimaryKey(depId);
                if(orgDep.getSfzxxq()==0){
                    resultBean=new ResultBean("您并不是中心员工！");
                }else{
                    resultBean= new ResultBean("200",depId);
                }
            }
        }
        BaseResponseUtil.responseAjaxJson(response, resultBean);
    }
}
